using System;
using System.Windows.Input;

namespace RandomSearch.VM {
    public class DelegateCommand<T> : ICommand {
        private readonly Predicate<T> canExecute;
        private readonly Action<T> execute;

        public DelegateCommand(Predicate<T> canExecute, Action<T> execute) {
            this.canExecute = canExecute;
            this.execute = execute;
        }

        public DelegateCommand(Action<T> execute) : this(null, execute) {}

        public string Name { get; set; }
        public string Text { get; set; }

        #region ICommand Members

        public virtual bool CanExecute(object parameter) {
            return canExecute == null || canExecute((T) parameter);
        }

        public event EventHandler CanExecuteChanged {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter) {
            execute((T) parameter);
        }

        #endregion
    }
}